package com.example.dao;

import com.example.entity.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
public interface OrderDao extends Mapper<Log> {

    @Insert("insert into `order` (order_id, ordercode, treename, number, username, shopper, ordertime, summoney, ordertype,orderstatus) " +
            "values (#{orderId},#{orderCode},#{treename},#{number},#{username},#{shopper},#{orderTime},#{summoney},#{ordertype},#{orderstatus})")
    void addOrder(Order order);

    @Select("select `order`.*, order_status.orderstatusvalue as orderstatusvalue from `order` left join order_status on `order`.orderstatus = order_status.orderstatus inner join user on `order`.username = user.username where ordertype = 0 and user.username = #{username}")
    List<OrderAndStatus> loadOrder(String username,@Param(value = "params") Params params);

    @Select("select `order`.*, order_status.orderstatusvalue as orderstatusvalue from `order` left join order_status on `order`.orderstatus = order_status.orderstatus where ordertype = 1")
    List<OrderAndStatus> loadChangeOrder(@Param(value = "params") Params params);

    @Select("select * from `order` where order_id = #{orderId}")
    Order findByOrderId(Integer orderId);

    //通过orderId修改order的订单状态
    @Update("update `order` set orderstatus = #{orderStatus} where order_id = #{orderId}")
    void updateStatus(Integer orderId,int orderStatus);

    List<OrderAndStatus> selectUserOrder(@Param(value = "params") Params params);

    List<OrderAndStatus> selectChangeOrder(@Param(value = "params") Params params);

    @Insert("insert into evaluate (evaluate_id, userId, ordercode, checkspeed, orderprice, usermeet, treeQuality, guideservice,status) " +
            "VALUES (#{evaluateId}, #{userId}, #{orderCode}, #{checkSpeed}, #{orderPrice}, #{userMeet}, #{treeQuality}, #{guideService},#{status})")
    void addEvaluate(Evaluate evaluateInfo);

    //通过userId查询订单信息
    @Select("select `order`.orderCode from `order` left join user on `order`.username = user.username where user.user_id = #{userId}")
    List<String> findOrderCodeByUserId(Integer userId);

    @Select("select * from evaluate left join evaluatestatus on evaluate.status = evaluatestatus.status where ordercode = #{item}")
    Evaluate findEvaluateByOrderCode(@Param(value = "item") String item);

    @Update("update evaluate set suggestion = #{suggestion} where ordercode = #{orderCode}")
    void updateEvaluate(Evaluate evaluate);

    List<EvaluateAndStatus> findeEvaluate(@Param(value = "params") Params params, @Param(value = "userId") Integer userId);

    @Delete("delete from evaluate where evaluate_id = #{evaluateId};")
    void cancelEvaluate(@Param(value = "evaluateId") Integer evaluateId);

    @Select("select `order`.ordercode from `order` where shopper = #{username}")
    List<String> findOrderCodeByShopper(@Param(value = "username") String username);

    List<EvaluateAndStatus> findEvaluateAndStatus(@Param(value = "params") Params params,@Param(value = "userId") Integer userId);

    @Update("update evaluate set status = 1 where evaluate_id = #{evaluateId}")
    void confirmInfo(@Param(value = "evaluateId") Integer evaluateId);

    List<EvaluateAndStatus> findAllUserEvaluate(@Param(value = "params") Params params);

    //通过id找到评价表对应的信息
    @Select("select * from evaluate where evaluate_id = #{evaluateId}")
    Evaluate findeEvaluateByEvaluateId(@Param(value = "evaluateId") Integer evaluateId);

    //通过orderCode找到商家名称
    @Select("select shopper from `order` where ordercode = #{orderCode}")
    String findShopperByorderCode(@Param(value = "orderCode") String orderCode);

    //商家交换订单取消操作
    @Delete("delete from `order` where order_id = #{orderId}")
    void cancelChange(@Param(value = "orderId") Integer orderId);

    //通过orderId找到orderCode
    @Select("select ordercode from `order` where order_id = #{orderId}")
    String findOrderCodeByOrderId(Integer orderId);

    //通过订单编号删除交换树苗信息
    @Delete("delete from changetree where ordercode = #{orderCode}")
    void delChangeTreeByOrderCode(String orderCode);

    //商家等价交换操作 更新 order表订单状态
    @Update("update `order` set orderstatus = 5 where ordercode = #{orderCode}")
    void equalPrice(String orderCode);

    //商家差额交换操作 更新order表订单状态
    @Update("update `order` set orderstatus = 6 where ordercode = #{orderCode}")
    void notEqual(String orderCode);

    //发起方商家确认等价交换订单操作
    @Update("update `order` set orderstatus = 9 where ordercode = #{orderCode}")
    void confirmEqualOrder(String orderCode);

    //发起方商家确认等价交换订单操作
    @Update("update `order` set orderstatus = 10 where ordercode = #{orderCode}")
    void confirmNotEqualOrder(String orderCode);


    //接收方商家确认等价交换订单操作
    @Update("update `order` set orderstatus = 4 where ordercode = #{orderCode}")
    void reconfirmEqualOrder(String orderCode);

    //接收方商家确认差额交换订单操作
    @Update("update `order` set orderstatus = 8 where ordercode = #{orderCode}")
    void reconfirmNotEqualOrder(String orderCode);

    //通过订单编号查询订单信息
    @Select("select * from `order` where ordercode = #{orderCode}")
    Order findByOrderCode(String orderCode);

    //通过订单编号找到订单id
    @Select("select order_id from `order` where ordercode = #{orderCode}")
    int findorderIdByorderCode(String orderCode);
}
